package com.tekj.cxqcb.view.Module.activity.ServiceOrder;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.tekj.cxqcb.MainBActivity;
import com.tekj.cxqcb.R;
import com.tekj.cxqcb.operation.BusinessDao;
import com.tekj.cxqcb.operation.resultBean.ShopServiceOrder;
import com.tekj.cxqcb.view.Module.activity.ServiceOrder.adapter.ServiceGoodsAdapter;
import com.tekj.cxqcb.view.Module.decoration.SpacesItemDecoration;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import common.base.activity.BaseActivity;
import common.base.operationHelper.Bean.BindingViewBean;
import common.tool.Tools;
import es.dmoral.toasty.Toasty;

public class OrderDetailsActivity extends BaseActivity {


    @BindView(R.id.img_back)
    ImageView imgBack;
    @BindView(R.id.tv_l)
    TextView tvL;
    @BindView(R.id.ll_back)
    LinearLayout llBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.iv_r)
    ImageView ivR;
    @BindView(R.id.tv_r)
    TextView tvR;
    @BindView(R.id.ll_right)
    LinearLayout llRight;
    @BindView(R.id.rl_title_bg)
    RelativeLayout rlTitleBg;
    @BindView(R.id.tv_type)
    TextView tvType;
    @BindView(R.id.tv_type2)
    TextView tvType2;
    @BindView(R.id.tv_top_click)
    TextView tvTopClick;
    @BindView(R.id.tv_call)
    TextView tvCall;
    @BindView(R.id.tv_user_name)
    TextView tvUserName;
    @BindView(R.id.tv_user_phone)
    TextView tvUserPhone;
    @BindView(R.id.tv_service_time)
    TextView tvServiceTime;
    @BindView(R.id.tv_service_car)
    TextView tvServiceCar;
    @BindView(R.id.tv_service_car_num)
    TextView tvServiceCarNum;
    @BindView(R.id.rv_main)
    RecyclerView rvMain;
    @BindView(R.id.tv_income)
    TextView tvIncome;
    @BindView(R.id.tv_income_id)
    TextView tvIncomeId;
    @BindView(R.id.tv_integral)
    TextView tvIntegral;
    @BindView(R.id.tv_reward)
    TextView tvReward;
    @BindView(R.id.ll_sz)
    LinearLayout llSz;
    @BindView(R.id.tv_sh_type)
    TextView tvShType;
    @BindView(R.id.tv_apply_time)
    TextView tvApplyTime;
    @BindView(R.id.tv_sh_reason)
    TextView tvShReason;
    @BindView(R.id.tv_end_time)
    TextView tvEndTime;
    @BindView(R.id.tv_result)
    TextView tvResult;
    @BindView(R.id.tv_result_reason)
    TextView tvResultReason;
    @BindView(R.id.ll_sh2)
    LinearLayout llSh2;
    @BindView(R.id.ll_sh)
    LinearLayout llSh;
    @BindView(R.id.tv_price)
    TextView tvPrice;
    @BindView(R.id.tv_Estimated_income)
    TextView tvEstimatedIncome;

    String orderid;
    BusinessDao businessDao;
    ShopServiceOrder.DataBean data;
    ServiceGoodsAdapter adapter;

    @Override
    public void init() {
        tvTitle.setText("订单详情");
        orderid = getIntent().getStringExtra("id");
        businessDao = new BusinessDao(activity, this);
        businessDao.BShopServiceOrderList(MainBActivity.User.getShopId(), orderid, 0, 0);
    }

    @Override
    public int setLayout() {
        return R.layout.activity_order_details;
    }

    @Override
    public void initLoadStart() {

    }

    @Override
    public void initLoadEnd() {

    }

    @Override
    public void initBindingView(Object mBean) {
        BindingViewBean bindingViewBean = (BindingViewBean) mBean;
        switch (bindingViewBean.getmEnumStatus()) {
            case 服务订单查询:
                ShopServiceOrder shopServiceOrder = (ShopServiceOrder) bindingViewBean.getBean();
                if (shopServiceOrder.getCode().equals("0000000")) {
                    try {
                        data = shopServiceOrder.getData().get(0);
                        setViewData();
                    } catch (Exception e) {
                        Toasty.normal(activity, "数据加载出错").show();
                    }

                }
                break;
        }


    }

    private String[] type = {"", "待付款", "进行中", "已完成", "已关闭"};
    String type2[] = {"", "平台审批", "平台同意", "平台拒绝", "已退款"};

    public void setViewData() {
        tvType.setText(type[data.getHeadStatusOne()]);
        tvType2.setText(data.getHeadStatusTwo());

        tvUserName.setText("联系人：" + data.getUserName());
        tvUserPhone.setText("联系电话：" + data.getUserPhone());
        tvServiceTime.setText("服务时间：" + data.getServiceTime());
        tvServiceCar.setText("到店车辆：" + data.getServiceCar());
        tvServiceCarNum.setText("车牌号码：" + data.getCarNum());

        tvPrice.setText("￥" + data.getOrderPrice());
        tvEstimatedIncome.setText("预计收入￥" + data.getPredictIncome());

        adapter = new ServiceGoodsAdapter(data.getShopService());
        rvMain.setLayoutManager(new LinearLayoutManager(activity));
        rvMain.setAdapter(adapter);
        rvMain.addItemDecoration(new SpacesItemDecoration(1));

        if (data.getAfterStatus() > 0) {

            llSh.setVisibility(View.VISIBLE);
            tvShType.setText(type2[data.getAfterStatus()]);
            tvApplyTime.setText(data.getAfterCreateTime());
            tvShReason.setText("退款原因：" + data.getAfterReason());

            if (data.getAfterStatus() > 1) {
                llSh2.setVisibility(View.VISIBLE);
                tvEndTime.setText(data.getPlatformExamineTime());

                tvResult.setText("退款申请" + (data.getAfterStatus() == 2 ? "已拒绝" : "已通过"));
                tvResultReason.setText("退款原因：" + data.getPlatformExamineRemark());
            }

        }
        if (data.getHeadStatusOne() > 2) {
            llSz.setVisibility(View.VISIBLE);
            tvIncome.setText("订单收入：￥" + data.getOrderIncome());
            tvIncomeId.setText("订单收支流水：" + data.getServiceWaterPayment());
            tvIntegral.setText("积分核销奖励：￥" + data.getIntegralAward());
            tvResult.setText("奖励收支流水：" + data.getStoreWaterPayment());
        }
    }

    @OnClick({R.id.ll_back, R.id.ll_right, R.id.tv_call})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ll_back:
                finish();
                break;
            case R.id.ll_right:
                Tools.callPhone(activity, "400");
                break;
            case R.id.tv_call:
                if (data != null)
                    Tools.callPhone(activity, data.getUserPhone());
                break;
        }
    }

}
